package com.sk.cardeal.act.home.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.widget.ImageView;
import android.widget.TextView;

import com.sk.cardeal.R;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.entity.OrderForm;
import com.sk.cardeal.utils.SnackUtils;
import com.sk.cardeal.view.TitleView;

import butterknife.BindView;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
 * Created by sk on 2018/1/31.
 */

@RuntimePermissions
public class ReachActivity extends BaseActivity {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.car_num)
    TextView carNum;
    @BindView(R.id.pep_name)
    TextView pepName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.phone_icon)
    ImageView phoneIcon;
    @BindView(R.id.order_time)
    TextView orderTime;
    @BindView(R.id.order_project)
    TextView orderProject;
    @BindView(R.id.ed_note)
    TextView edNote;
    private OrderForm orderForm;

    @Override
    protected int getContentView() {
        return R.layout.activity_reach;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setMyCallBack(this);
        orderForm = getIntent().getParcelableExtra("data");
        carNum.setText(orderForm.getCardNumber());
        tvPhone.setText(orderForm.getPhone());
        pepName.setText(orderForm.getName());
        orderTime.setText(orderForm.getConventionTime());
        orderProject.setText(orderForm.getTypeName());
        edNote.setText(orderForm.getNote());
    }

    @OnClick(R.id.phone_icon)
    public void onViewClicked() {
        ReachActivityPermissionsDispatcher.startCallWithPermissionCheck(ReachActivity.this);
    }


    @NeedsPermission({Manifest.permission.CALL_PHONE})
    void startCall() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + tvPhone.getText().toString());
        intent.setData(data);
        startActivity(intent);
    }

    // 向用户说明为什么需要这些权限（可选）
    @NeedsPermission({Manifest.permission.CALL_PHONE})
    void showRationaleForPhone(final PermissionRequest request) {
        new AlertDialog.Builder(mContext)
                .setMessage("源圈需要使用您的电话！")
                .setPositiveButton("允许", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .show();
    }

    // 用户拒绝授权回调（可选）
    @NeedsPermission({Manifest.permission.CALL_PHONE})
    void showDeniedForPhone() {
        SnackUtils.showError(mRootView, "您拒绝了电话权限！");
    }


    // 用户勾选了“不再提醒”时调用（可选）
    @NeedsPermission({Manifest.permission.CALL_PHONE})
    void showNeverAskForPhone() {
        new AlertDialog.Builder(mContext)
                .setMessage("您已禁止app使用您的电话,请到设置中开启。")
                .setPositiveButton("允许", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BASE) {
                            // 进入设置系统应用权限界面
                            Intent intent = new Intent(Settings.ACTION_SETTINGS);
                            startActivity(intent);
                            return;
                        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {// 运行系统在5.x环境使用
                            // 进入设置系统应用权限界面
                            Intent intent = new Intent(Settings.ACTION_SETTINGS);
                            startActivity(intent);
                            return;
                        }
                    }
                })
                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }


    @SuppressLint("NeedOnRequestPermissionsResult")
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        ReachActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}
